package com.syg.swagger;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author syg
 */
@SpringBootApplication
public class SwaggerBoot implements BeanFactoryPostProcessor {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerBoot.class, args);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        for (int i = 0; i < 10; i++) {
            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo()).pathMapping("/for" + i + "/")
                    // 分组
                    .groupName("swagger3.0与之前版本不同" + i)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.syg.swagger.controller"))
                    .build();
            beanFactory.registerSingleton("user"+i, docket);
        }
    }

    /**
     * 通过apiInfo()属性配置文档信息
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
        return new ApiInfo(
                // 标题
                "Swagger学习",
                // 描述
                "学习演示如何配置Swagger",
                // 版本
                "v1.0",
                // 组织链接
                "http://terms.service.url/组织链接",
                // 联系人信息
                contact,
                // 许可
                "Apach 2.0 许可",
                // 许可连接
                "许可链接",
                // 扩展
                new ArrayList<>()
        );
    }


    // //添加bean
    // public void addBean(String beanName, Class<?> beanClass) {
    //     BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
    //
    //     BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(beanClass);
    //     BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
    //     if (!beanDefinitionRegistry.containsBeanDefinition(beanName)) {
    //         beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition);
    //     }
    // }
    // //移除bean
    // public void removeBean(String beanName) {
    //     BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
    //     beanDefinitionRegistry.getBeanDefinition(beanName);
    //     beanDefinitionRegistry.removeBeanDefinition(beanName);
    // }

}
